/*
 *    This file is part of BlobDB.
 *
 *    BlobDB is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    BlobDB is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with BlobDB.  If not, see <http://www.gnu.org/licenses/>.
 *    
 *    Copyright(c) 2011 Michael Kankkonen
 *    http://www.vnetcon.org
 */


package org.vnetcon.blobdb.database.backend.gae;

import java.util.ArrayList;
import java.util.List;

import org.vnetcon.blobdb.database.backend.QueryInterface;
import org.vnetcon.blobdb.database.backend.TableDataInterface;
import org.vnetcon.blobdb.database.backend.TableIDInterface;
import org.vnetcon.blobdb.database.backend.TableInterface;
import org.vnetcon.blobdb.database.backend.gae.jdo.Table;
import org.vnetcon.blobdb.database.backend.gae.jdo.TableData;
import org.vnetcon.blobdb.database.backend.gae.jdo.TableID;

import javax.jdo.Query;

public class GAEQuery implements QueryInterface {

	Query nativeQuery = null;
	Class<?> clazz = null;
	
	int TABLE_TYPE = 1;
	int TABLEDATA_TYPE = 2;
	int TABLEID_TYPE = 3;
	int execute_type = TABLE_TYPE;
	
	public GAEQuery(Query nativeQuery, Class<?> clazz){
		this.nativeQuery = nativeQuery;
		this.clazz = clazz;
		String name = clazz.getName();
		if(name.indexOf("TableData") > -1){
			execute_type = TABLEDATA_TYPE;
		}
		if(name.indexOf("TableID") > -1){
			execute_type = TABLEID_TYPE;
		}
	}

	
	public Query getNativeQuery(){
		return this.nativeQuery;
	}
	
	@Override
	public List<?> execute() {
		
		if(this.execute_type == TABLE_TYPE){
			List<Table> list = (List<Table>)this.nativeQuery.execute();
			ArrayList<TableInterface> retList = new ArrayList<TableInterface>();
			int i = 0;
			while(i < list.size()){
				GAETable gaet = new GAETable(list.get(i));
				retList.add(gaet);
				i++;
			}
			return retList;
		}

		if(this.execute_type == TABLEID_TYPE){
			List<TableID> list = (List<TableID>)this.nativeQuery.execute();
			ArrayList<TableIDInterface> retList = new ArrayList<TableIDInterface>();
			int i = 0;
			while(i < list.size()){
				GAETableID gaet = new GAETableID(list.get(i));
				retList.add(gaet);
				i++;
			}
			return retList;
		}

		if(this.execute_type == TABLEDATA_TYPE){
			List<TableData> list = (List<TableData>)this.nativeQuery.execute();
			ArrayList<TableDataInterface> retList = new ArrayList<TableDataInterface>();
			int i = 0;
			while(i < list.size()){
				GAETableData gaet = new GAETableData(list.get(i));
				retList.add(gaet);
				i++;
			}
			return retList;
		}
		
		return null;
	}

	@Override
	public void setFilter(String filter) {
		this.nativeQuery.setFilter(filter);
	}

	@Override
	public void declareParameters(String params) {
		this.nativeQuery.declareParameters(params);
	}

	@Override
	public List<?> execute(Object o1) {
		
		if(this.execute_type == TABLE_TYPE){
			List<Table> list = (List<Table>)this.nativeQuery.execute(o1);
			ArrayList<TableInterface> retList = new ArrayList<TableInterface>();
			int i = 0;
			while(i < list.size()){
				GAETable gaet = new GAETable(list.get(i));
				retList.add(gaet);
				i++;
			}
			return retList;
		}

		if(this.execute_type == TABLEID_TYPE){
			List<TableID> list = (List<TableID>)this.nativeQuery.execute(o1);
			ArrayList<TableIDInterface> retList = new ArrayList<TableIDInterface>();
			int i = 0;
			while(i < list.size()){
				GAETableID gaet = new GAETableID(list.get(i));
				retList.add(gaet);
				i++;
			}
			return retList;
		}

		if(this.execute_type == TABLEDATA_TYPE){
			List<TableData> list = (List<TableData>)this.nativeQuery.execute(o1);
			ArrayList<TableDataInterface> retList = new ArrayList<TableDataInterface>();
			int i = 0;
			while(i < list.size()){
				GAETableData gaet = new GAETableData(list.get(i));
				retList.add(gaet);
				i++;
			}
			return retList;
		}
		
		return null;
	}

	@Override
	public List<?> execute(Object o1, Object o2) {
		
		if(this.execute_type == TABLE_TYPE){
			List<Table> list = (List<Table>)this.nativeQuery.execute(o1, o2);
			ArrayList<TableInterface> retList = new ArrayList<TableInterface>();
			int i = 0;
			while(i < list.size()){
				GAETable gaet = new GAETable(list.get(i));
				retList.add(gaet);
				i++;
			}
			return retList;
		}

		if(this.execute_type == TABLEID_TYPE){
			List<TableID> list = (List<TableID>)this.nativeQuery.execute(o1, o2);
			ArrayList<TableIDInterface> retList = new ArrayList<TableIDInterface>();
			int i = 0;
			while(i < list.size()){
				GAETableID gaet = new GAETableID(list.get(i));
				retList.add(gaet);
				i++;
			}
			return retList;
		}

		if(this.execute_type == TABLEDATA_TYPE){
			List<TableData> list = (List<TableData>)this.nativeQuery.execute(o1, o2);
			ArrayList<TableDataInterface> retList = new ArrayList<TableDataInterface>();
			int i = 0;
			while(i < list.size()){
				GAETableData gaet = new GAETableData(list.get(i));
				retList.add(gaet);
				i++;
			}
			return retList;
		}
		
		return null;
	}

	@Override
	public void setOrdering(String order) {
		this.nativeQuery.setOrdering(order);
	}

	@Override
	public void setRange(int start, int end) {
		this.nativeQuery.setRange(start, end);
	}

}
